(function() {

    var AutocompleteCacheClass = function() {

        /**
         * @type Object
         * @private
         */
        this._cache = { };

        /**
         * @type Number
         * @private
         */
        this._cacheSize = 0;

        // -------------------------------------------------------
        // -                        LOGIC                        -
        // -------------------------------------------------------

        //
        // API
        //

        /**
         * Add new value with specified key to cache.
         *
         * @param {String} key Cache entry key.
         * @param {Object} value Cache entry value.
         */
        this.put = function(key, value) {
            this._cache[key.toLowerCase()] = value;
            ++this._cacheSize;
        };

        /**
         * Search value in cache by specified key.
         *
         * @param {String} key Cache entry key or it's part.
         * @return {Object} Cache entry value.
         */
        this.get = function(key) {
            key = key.toLowerCase();

            var result = this._cache[key];

            if (result) return result;

            var
            i = key.length,
            subValue, j;

            while (--i > 0) {
                subValue = this._cache[key.substr(0, i)];

                if (!subValue) continue;

                result = [];

                for (j = 0; j < subValue.length; ++j) {
                    if (subValue[j].title.toLowerCase().indexOf(key) == 0) {
                        result.push(subValue[j]);
                    }
                }

                return ((result.length) > 0 ? result : null);
            }

            return null;
        };

    };

    juliet.registerModule('juliet.ui.input.withPopupList.autocomplete', 'AutocompleteCache-0.1', AutocompleteCacheClass);

})();
